/**
 * Created by Administrator on 2015/5/14.
 */
var express = require('express');
var router = express.Router();
var MongoClient = require('mongodb').MongoClient;

var mongoDB = null;

var mongoURL = "mongodb://127.0.0.1:27017/order";

MongoClient.connect(mongoURL, function (err, db) {
    if (err)console.error("mongodb init error", err);
    mongoDB = db;
    console.log("mongodb init~");
});

router.get('/order', function (req, res) {
    var date = req.param("date");
    var name = req.param("name");
    var meal = req.param("meal");
    var uid =  req.param("uid");
    var price =  req.param("price");
    var time = getTimeStr();
    var obj = {date: date, name: name, meal: meal, time: time, show: '1', payment: '0',uid:uid,price:price,orderDate:new Date()};
    console.log(JSON.stringify(obj));
    mongoDB.collection("orders").insert(obj, {}, function (err) {
        var result = null;
        if (err) {
            result = {status: "error"};
        } else {
            result = {status: "ok"};
        }
        res.send(result);
    });

});


router.get('/queryOrders', function (req, res) {
    var date = req.param("date");
    console.log(date);
    mongoDB.collection("orders").find({date: date, show: "1"}, {sort: {'name': 1}}).toArray(function (err, docs) {
        if (err) {
            res.send([]);
        } else {
            res.send(docs);
        }
    });
});

router.get('/queryMeals', function (req, res) {
    mongoDB.collection("meal").find({type: '2'}, {sort: {'price': 1}}).toArray(function (err, meals) {
        if (err) {
            res.send([]);
        } else {
            res.send(meals);
        }
    });
});


router.get('/deleteOrder', function (req, res) {
    var date = req.param("date");
    var orderId = req.param("orderId");
    var BSON = require('mongodb').BSONPure;
    var query = {date: date, _id: BSON.ObjectID.createFromHexString(orderId)};
    console.log(JSON.stringify(query));
    mongoDB.collection("orders").update(query, {$set: {show: '0'}}, function (err, r) {
        //remove(query, {w: 1}, function (err, r) {
        var result = null;
        if (err) {
            result = {status: "error"};
        } else {
            result = {status: "ok"};
        }
        res.send(result);
    });

});

router.get('/payOrder',function(req,res){
    var date = req.param("date");
    var orderId = req.param("orderId");
    var BSON = require('mongodb').BSONPure;
    var query = {date: date, _id: BSON.ObjectID.createFromHexString(orderId)};
    console.log(JSON.stringify(query));
    mongoDB.collection("orders").update(query, {$set: {payment: '1'}}, function (err, r) {
        //remove(query, {w: 1}, function (err, r) {
        var result = null;
        if (err) {
            result = {status: "error"};
        } else {
            result = {status: "ok"};
        }
        res.send(result);
    });
})


var getTimeStr = function () {
    var date = new Date();
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var day = date.getDate();
    var hour = date.getHours();
    var minute = date.getMinutes();
    var dateStr = year + "-" + month + "-" + day + " " + hour + ":" + minute;
    return dateStr;
}

module.exports = router;